#include <iostream>
#include <stdlib.h>
#include <algorithm>

#include "libmri.h"

using namespace std;

int main (int argc, char **argv)
{
	shm_array_t *shm_calib = 0, size_output, *shm_output = 0;
	char *output = 0;

	if (0 == shm_calib){
		if (get_shm_array("calib", &shm_calib)){
			perror("calib");
			return -1;
		}
	}

	if (0 == output){
		output = strdup("output");
	}



	int csize[4] = {shm_calib->dims[0], shm_calib->dims[1],
	                shm_calib->dims[2], shm_calib->dims[3]};

	cplxf *calib; cuda (Malloc (&calib,sizeof(*calib) *
	                            csize[0]*csize[1]*csize[2]*csize[3]));
	cuda (Memcpy(calib, shm_calib->data.c,
	             sizeof(*calib) * csize[0]*csize[1]*csize[2]*csize[3],
	             cudaMemcpyHostToDevice));

	int ksize[3] = {7, 7, 7};


	cplxf *V = 0; int nv = -1;

	calib3d_culanczos (V, nv, calib, csize, ksize, 7*7*7, 0.02);

	
	size_output.type = shm_calib->type;
	size_output.ndims = 2;
	size_output.dims[0] = 7*7*7*csize[3];
	size_output.dims[1] = nv;

	if (make_shm_array (output, &size_output, &shm_output)){
		perror(output);
		return -1;
	}

	memcpy (shm_output->data.c, V, 7*7*7*csize[3]*nv*sizeof(*V));

	return 0;
}
